<?php

namespace App\Models;

use App\Util\Url;
use Illuminate\Database\Eloquent\Model;

class PatientLog extends Model
{
    protected $table = "c_patient_log";

    /**
     * @param $filter
     * 获取患教内容记录
     */
    public function getPatientContent($filter)
    {
        $lit = self::where([['work_id', '=', $filter['work_id']], ['doctor_id', '=', $filter['user_id']]])->orderByDesc("id")->simplePaginate(15)->toArray();

        if(empty($lit['data'])){
            return [];
        }
        $art = new Article();
        $cou = new Course();
        $vist_user = new VisitUser();
        $url = Url::getHttpHost();
        foreach ($lit['data'] as &$v) {
            if ($v['type'] == 1) {
                $art_info = $art->where([['id','=',$v['patient_id']]])->select("article_title","art_img","views","likes")->first();
                if($art_info){
                    $art_info->art_img = $url.'upload/'.$art_info->art_img;
                    $v['art_info'] = $art_info;
                }else{
                    self::where([['id','=',$v['id']]])->delete();
                    continue;
                }
            } elseif ($v['type'] == 2) {
                $art_info = $cou->where([['id','=',$v['patient_id']]])->first();
                if($art_info){
                $art_info->course_img = $url.'upload/'.$art_info->course_img;
                $v['art_info'] = $art_info;
                }else{
                    self::where([['id','=',$v['id']]])->delete();
                    continue;
                }
            }
            $v['view_num'] = $vist_user->where([['pat_log_id','=',$v['id']],['is_read','=',0]])->count();
            $v['total_num'] = $vist_user->where([['pat_log_id','=',$v['id']]])->count();
        }
        return $lit;
    }



}
